package com.fenghongzhang.day012;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
    private TextView info;

    private static final String TAG = "Main2Activity";
    private EditText edit;

   private Handler handler = new Handler(){
       @SuppressLint("HandlerLeak")
       @Override
       public void handleMessage(@NonNull Message msg) {
           super.handleMessage(msg);

           Log.i(TAG, "handleMessage: ");
            String s = (String) msg.obj;
            int i = msg.what;
            if(msg.what == 1){
                int i1 = msg.arg1;
                int i2 = msg.arg2;
                Bundle b = msg.getData();
                String name = b.getString("name");
                Log.i(TAG, "handleMessage: "+i1+i2+name);
            }else if(msg.what == 2){
                int i1 = msg.arg1;
                int i2 = msg.arg2;
                Bundle b = msg.getData();
                String name = b.getString("name");
                Log.i(TAG, "handleMessage: "+i1+i2+name);
            }

       }
   };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        info = (TextView) findViewById(R.id.info);
        edit = (EditText) findViewById(R.id.edit);


        /**
         * 注意按钮的可用性.enable这个属性
         */

        //输入的监听器事件
       edit.addTextChangedListener(new TextWatcher() {
           /**
            * 文字改变前
            * @param s
            * @param start
            * @param count
            * @param after
            */
           @Override
           public void beforeTextChanged(CharSequence s, int start, int count, int after) {

           }

           /**
            * 文字改变中调用
            * @param s
            * @param start
            * @param before
            * @param count
            */
           @Override
           public void onTextChanged(CharSequence s, int start, int before, int count) {

           }

           /**
            * 文字改变后调用
            * @param s
            */
           @Override
           public void afterTextChanged(Editable s) {

           }
       });

    }

    public void requestPost(View view) {

        Log.i(TAG, "requestPost: ");
//        new MyThread().start();
        Message obtain = Message.obtain();
        //数据放入到message
        obtain.what = 1;
        obtain.obj = "哈哈哈1111";
        obtain.arg1 = 1;
        obtain.arg2 = 1;

        Bundle bundle = new Bundle();
        bundle.putString("name","小明");
        obtain.setData(bundle);

//        handler.sendMessage(obtain);

        //2,是从对象池中,获取一个对象
        Message obtain2 = Message.obtain();
        //数据放入到message
        obtain2.what = 2;
        obtain2.obj = "哈哈哈2222";
        obtain2.arg1 = 2;
        obtain2.arg2 = 2;

        Bundle bundle2 = new Bundle();
        bundle2.putString("name","小明222");
        obtain2.setData(bundle2);
        handler.sendMessage(obtain2);

    }

    class MyThread extends Thread{

        @Override
        public void run() {
            super.run();

            try {
                URL url = new URL("http://www.kuaidi100.com/query");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                urlConnection.setRequestMethod("POST");
//                urlConnection.setReadTimeout(3*1000);
//                urlConnection.setConnectTimeout(5*1000);

                //默认输出是关闭的.我们要打开
                urlConnection.setDoOutput(true);
                urlConnection.setDoInput(true);
                OutputStream outputStream = urlConnection.getOutputStream();
                String s = "type=yuantong&postid=11111111111";
                outputStream.write(s.getBytes());
                outputStream.flush();

                if(urlConnection.getResponseCode() == 200){
                    InputStream inputStream = urlConnection.getInputStream();

                    StringBuilder stringBuilder = new StringBuilder();
                    //把一个流转换成文字
                    byte[] bytes =  new byte[1024];
                    int len =0;
                    while ((len = inputStream.read(bytes)) != -1){
                        String s1 = new String(bytes, 0, len);
                        stringBuilder.append(s1);
                    }
                    String string = stringBuilder.toString();
                    Log.i(TAG, "test: "+string);

                    Gson gson = new Gson();
                    City city = gson.fromJson(string, City.class);
                    List<City.DataBean> data = city.getData();
                    for (int i = 0; i < data.size(); i++) {
                        City.DataBean dataBean = data.get(i);
                        final String s2 = dataBean.getContext();
                        Log.i(TAG, "run: "+s2);
//                        info.setText(context);//



                        //1 .new 对象
//                        Message message = new Message();
                        //2,是从对象池中,获取一个对象

//                        handler.sendMessage(obtain2);

                        //用handler来发送消息
//                        handler.post(new Runnable() {
//                            @Override
//                            public void run() {
//                                info.setText(s2);
//                            }
//                        });
//                        Log.i(TAG, "run: 11");
//                        //可以延时的发送
//                        handler.postDelayed(new Runnable() {
//                            @Override
//                            public void run() {
//                                Log.i(TAG, "run: 22");
//                                info.setText(s2);
//                            }
//                        },3000);
                        //可以定时发送
//                        handler.postAtTime(new Runnable() {
//                            @Override
//                            public void run() {
//                                info.setText(s2);
//                            }
//                        },1321231231200);

                    }
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }
}
